package com.health.db.dao.user.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.user.IPatientListDao;
import com.health.db.mapper.PatientListMapper;
import com.health.db.model.PatientList;
import com.health.db.model.PatientListExample;
import com.health.db.model.PatientListTemp;
import com.health.redis.RedisConstants;

public class PatientListDaoImpl extends ServiceInterface implements IPatientListDao {
	
	private static String TAG="PatientListDaoImpl";
 
	private static Logger logger = Logger.getLogger(PatientListDaoImpl.class);
	PatientListMapper patientListMapper;
	String selectByPatId=":patientId:";
	String selectByUserId=":userId:";
	String selectByPatIdTemp=":patientIdT:";
	
	/**
	 * 		userid 用戶id 
	 * 		patientid 就诊人id    无查询条件
	 * @param paList
	 * @return
	 */
	@Override
	public List<PatientListTemp> selectByPatientListSelective(PatientList paList) {
		// TODO Auto-generated method stub
		logger.info(TAG+"查询就诊人信息开始");
		getRedisOperatiorLogic();
		String key =RedisConstants.PatientListMapper+selectByUserId+paList.getUserid();
		
		if (null!=paList.getPatientid()&&paList.getPatientid()!=0) {
			key = RedisConstants.PatientListMapper+selectByPatIdTemp+paList.getPatientid().longValue();
		}
		
		Object object=null;
		List<PatientListTemp> patientLists = null;
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{	
			object = redisOperatiorLogic.readObject(key, getPublicRedisGroupNo("1"));
		 }
	     if(null!=object){
	    	 if (!object.equals("")) {
	    		 patientLists =  (List<PatientListTemp> ) object;
			}
	     }else{
 
             patientLists=patientListMapper.selectByPatientListSelective(paList);
             if(redisOperatiorLogic.isJedisIsConnected())
				{redisOperatiorLogic.setEx(key, patientLists,  getOrderRedisGroupNo("1"));}
		 }
		logger.info(TAG+"查询就诊人信息结束");
		return patientLists;
	}

	@Override
	public int deleteByPatId(Long patId) {
		// TODO Auto-generated method stub
		logger.info(TAG+"删除就诊人信息开始");
		int result=-1;
		
		getRedisOperatiorLogic();
		String key =RedisConstants.PatientListMapper+selectByPatId+patId;
		PatientListExample example=new PatientListExample();
		PatientListExample.Criteria criteria=example.createCriteriaInternal();
		criteria.andPatientidEqualTo(patId);
		result=patientListMapper.deleteByExample(example);
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteObject(key, getOrderRedisGroupNo("1"));
		}
		
		logger.info(TAG+"删除就诊人信息结束");
		return result;
	}

	@Override
	public int addPatientList(PatientList record) {
		// TODO Auto-generated method stub
		logger.info(TAG+"新增就诊人信息开始");
		int result=-1;

		getRedisOperatiorLogic();
		result=patientListMapper.insert(record);
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			String key1 =RedisConstants.PatientListMapper+selectByUserId+record.getUserid();
			redisOperatiorLogic.deleteObject(key1, getOrderRedisGroupNo("1"));
		}
	
		logger.info(TAG+"新增就诊人信息结束");
		return result;
	}

	@Override
	public int updatePatientList(PatientList record,PatientListExample example) {
		// TODO Auto-generated method stub
		logger.info(TAG+"更新就诊人信息开始");
		int result=-1;
		getRedisOperatiorLogic();
		result=patientListMapper.updateByExampleSelective(record, example);
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			String key =RedisConstants.PatientListMapper+selectByPatId+record.getPatientid().longValue();
			String key1 =RedisConstants.PatientListMapper+selectByUserId+this.selectByPatientId(record.getPatientid()).getUserid().longValue();
			String key2 =RedisConstants.PatientListMapper+selectByPatIdTemp+record.getPatientid().longValue();
			redisOperatiorLogic.deleteObject(key, getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(key1, getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(key2, getOrderRedisGroupNo("1"));
		}
	
		logger.info(TAG+"更新就诊人信息结束");
		return result;
	}

	/**
	 * patientId 主键
	 */
	@Override
	public PatientList selectByPatientId(Long patientId) {
		// TODO Auto-generated method stub
		logger.info(TAG+"查询就诊人信息开始");
		getRedisOperatiorLogic();
		String key =RedisConstants.PatientListMapper+selectByPatId+patientId;
		Object object=null;
		
		PatientList patientList=null;
	
		if(redisOperatiorLogic.isJedisIsConnected())
		{	
			object = redisOperatiorLogic.readObject(key, getPublicRedisGroupNo("1"));
		 }
	     if(null!=object){
	    	 
	    	 if (!object.equals("")) {
	    		 patientList =  (PatientList) object;
	    	 }
	     }else{
	    	 patientList= patientListMapper.selectByPatientId(patientId);
	    	 if(redisOperatiorLogic.isJedisIsConnected())
	    	 {
	    		 redisOperatiorLogic.setEx(key, patientList, getOrderRedisGroupNo("1"));
	    	 }
	     }

		logger.info(TAG+"查询就诊人信息结束");
		return patientList;
	}

	/*****************************************************************************************/
	
	public PatientListMapper getPatientListMapper() {
		return patientListMapper;
	}

	public void setPatientListMapper(PatientListMapper patientListMapper) {
		this.patientListMapper = patientListMapper;
	}

}
